using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;
using System.Linq;
using System.Collections.ObjectModel;

namespace GPUAPP2
{
    public class FDataDisplayManager
    {
        private readonly DataGrid dataGrid;
        private ObservableCollection<FDataRowData> displayData;

        public FDataDisplayManager(DataGrid dataGrid)
        {
            this.dataGrid = dataGrid;
            this.displayData = new ObservableCollection<FDataRowData>();
            InitializeColumns();
        }

        private void InitializeColumns()
        {
            dataGrid.Columns.Clear();

            // 添加行号列
            var rowNumberColumn = new DataGridTextColumn
            {
                Header = "行号",
                Binding = new Binding("RowNumber") { StringFormat = "F{0}" }
            };
            dataGrid.Columns.Add(rowNumberColumn);

            // 添加F0-F29列
            for (int i = 0; i < 30; i++)
            {
                var column = new DataGridTextColumn
                {
                    Header = $"F{i}",
                    Binding = new Binding($"Values[{i}]")
                };
                dataGrid.Columns.Add(column);
            }
        }

        public void UpdateDisplay(IEnumerable<int[]> newData, int startIndex = 0)
        {
            var newRows = newData.Select((values, index) => new FDataRowData
            {
                RowNumber = startIndex + index + 1,
                Values = values
            });

            // 在UI线程上更新数据
            dataGrid.Dispatcher.Invoke(() =>
            {
                if (startIndex == 0)
                {
                    displayData.Clear();
                }

                foreach (var row in newRows)
                {
                    displayData.Add(row);
                }

                if (dataGrid.ItemsSource == null)
                {
                    dataGrid.ItemsSource = displayData;
                }
            });
        }

        public void Clear()
        {
            dataGrid.Dispatcher.Invoke(() =>
            {
                displayData.Clear();
            });
        }
    }

    public class FDataRowData
    {
        public int RowNumber { get; set; }
        public int[] Values { get; set; }
    }
} 